package com.deliveryservicesystem.app.validation;

import com.deliveryservicesystem.model.Billing;
import com.deliveryservicesystem.model.Customer;
import com.deliveryservicesystem.model.Delivery;
import com.jgoodies.binding.value.AbstractValueModel;
import com.jgoodies.common.base.Strings;
import com.jgoodies.validation.ValidationResult;
import com.jgoodies.validation.Validator;
import com.toedter.calendar.JDateChooser;
import java.util.Date;

/**
 * File Name: ProductValidator.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.app.validation
 * Author: Lance
 * Date: Jun 29, 2012
 * Editor:
 */
public class SubscripionValidator extends AbstractValueModel implements Validator<ValidationResult>
{

    private static final long serialVersionUID = 1L;
    private JDateChooser subscriptionDate;
    private String baseAmount;
    private String totalAmount;
    private Billing billing;
    private Customer customer;
    private Delivery delivery;

    public SubscripionValidator(JDateChooser subscriptionDate, String baseAmount, String totalAmount, Billing billing, Customer customer, Delivery delivery)
    {
        this.subscriptionDate = subscriptionDate;
        this.baseAmount = baseAmount;
        this.totalAmount = totalAmount;
        this.billing = billing;
        this.customer = customer;
        this.delivery = delivery;
    }

    public String getBaseAmount()
    {
        return baseAmount;
    }

    public void setBaseAmount(String baseAmount)
    {
        this.baseAmount = baseAmount;
    }

    public Billing getBilling()
    {
        return billing;
    }

    public void setBilling(Billing billing)
    {
        this.billing = billing;
    }

    public Customer getCustomer()
    {
        return customer;
    }

    public void setCustomer(Customer customer)
    {
        this.customer = customer;
    }

    public Delivery getDelivery()
    {
        return delivery;
    }

    public void setDelivery(Delivery delivery)
    {
        this.delivery = delivery;
    }

    public JDateChooser getDeliveryDate()
    {
        return subscriptionDate;
    }

    public void setDeliveryDate(JDateChooser deliveryDate)
    {
        this.subscriptionDate = deliveryDate;
    }

    public String getTotalAmount()
    {
        return totalAmount;
    }

    public void setTotalAmount(String totalAmount)
    {
        this.totalAmount = totalAmount;
    }

    @Override
    public Object getValue()
    {
        return this;
    }

    @Override
    public void setValue(Object newValue)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ValidationResult validate(ValidationResult validationTarget)
    {
        if (subscriptionDate.getDate().after(new Date()))
        {
            validationTarget.addError("Subscriptions can not be set in future");
        }
        if (Strings.isEmpty(baseAmount.trim()))
        {
            validationTarget.addError("Base amount required");
        }
        try
        {
            Double.parseDouble(baseAmount.trim());
        }
        catch (Exception e)
        {
            validationTarget.addError("Base amount must be numeric");
        }
        if (Strings.isEmpty(totalAmount.trim()))
        {
            validationTarget.addError("Total amount required");
        }
        try
        {
            Double.parseDouble(totalAmount.trim());
        }
        catch (Exception e)
        {
            validationTarget.addError("Total amount must be numeric");
        }
        if (billing == null)
        {
            validationTarget.addError("Please select a billing number");
        }
        if (customer == null)
        {
            validationTarget.addError("Please select a customer number");
        }
        if (delivery == null)
        {
            validationTarget.addError("Please select a delivery number");
        }
        return validationTarget;
    }
}